
var divdisp=null;
var dateop=null;

var clickcnt=0;
function caldate_onclick(calday,calmonth,calyear)
{

calmonthp=parseInt(calmonth)+1;
if (calmonthp==13)calmonthp=1;
document.getElementById(dateop).value=calday + "/"+ calmonthp + "/"+ calyear;
document.getElementById(divdisp).innerHTML="";
}

function showCalendar(day,month,year) 
         {

	 document.getElementById(divdisp).style.top=document.getElementById(dateop).style.top;
	  
		date = new Date(); 
  if(month==null||year==null)
  {
        month = date.getMonth();
         year = date.getFullYear();
   }
         
		 if (day==null) 
		 
		 day = date.getDate();
		 
         months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
       
  
         this_month = new Date(year, month, 1);
         next_month = new Date(year, month + 1, 1);       
         first_week_day = this_month.getDay();
         days_in_this_month = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
         
		 calendar_html ="";
		 
		 nextmonth=month+1;
		 nextyear=year;
	     prevmonth=month-1;
		 prevyear=year;
		
		
		
		if(nextmonth==13)  
		{
		  nextmonth=1;
		  nextyear=year+1;
		 } 

	 		if(prevmonth==0)  
		{
		  prevmonth=12;
		  prevyear=year-1;
		 } 
		 month_name= months[month];
		 
		 if(month==12)
		 {
		  month_name='January';
		  year++;	
		 }
		
		 
         calendar_html += '<Div id="datepicker"  onClick="dateclicked()"><table width="158" class="Borders alwaysTop"><tr>';
		 calendar_html +='<td class="Title"><a href="javascript:void(0)"> <img src="img/calendar/leftarow.gif" border=0 align="absmiddle" ';
       
	   //***************************************************************************************************
	   //left	
	    calendar_html +="onclick='javascript:showCalendar("+ day +","+ prevmonth +","+ prevyear +")'";
	   //*************************************************************************************************** 
		 
		 
		 
		 calendar_html +='></a></td nowrap><td colspan="5" align="center" class="Title" nowrap>' +month_name + ' ' + year + '&nbsp;&nbsp;&nbsp;<a href ="javascript:void(0)" style="color:red;" onclick="hidecal()">X</a></td>';

		  calendar_html +='<td class="Title" align="right"> <a href="javascript:void(0)"> <img src="img/calendar/rightarow.gif" border=0 align="absmiddle" ';

		  
		//***************************************************************************************************  
		//right	  
		  calendar_html +="onclick='javascript:showCalendar("+ day +","+ nextmonth  +","+ nextyear +")'";
		//***************************************************************************************************  
		  

         calendar_html += '></a></td></tr><tr><td align="center" class="Head">S</td><td align="center" class="Head">M</td><td align="center" class="Head">T</td><td align="center" class="Head">W</td><td align="center" class="Head">T</td><td align="center" class="Head">F</td><td align="center" class="Head">S</td></tr>';
				  
         calendar_html += '<tr>';
          
         //Fill the first week of the month with the appropriate number of blanks.       
         for(week_day = 0; week_day < first_week_day; week_day++)
            {
            calendar_html += '<td>&nbsp;</td>';   
            }
            
         week_day = first_week_day;
         for(day_counter = 1; day_counter <= days_in_this_month; day_counter++)
            {
            week_day %= 7;
            
            if(week_day == 0)
               calendar_html += '</tr><tr>';
            
            //Do something different for the current day.
            if(day == day_counter)   
calendar_html += '<td align="center"><a href="javascript:void(0)" class="Today" onclick="caldate_onclick(' + day_counter +','+ month+','+ year +')" >' +  day_counter + '</a> </td>';
            else{
calendar_html += '<td align="center"><a href="javascript:void(0)" class="NormalDay" onclick="caldate_onclick(' + day_counter +','+ month+','+ year +')" >' +  day_counter + '</a> </td>';
            }
            week_day++;
            }
            
         calendar_html += '</tr>';
         calendar_html += '</table></div>';
         
 
            
  document.getElementById(divdisp).innerHTML=calendar_html;

}

function popcal(dividx,opidx)
{hidecal();
 clickcnt=1;
 
 divdisp=dividx;
 dateop=opidx;
 showCalendar(null,null,null);
}

function iclicked()
{
	if(divdisp){
	clickcnt++;
    if (clickcnt>=3) hidecal()
		 
	}
}

function dateclicked()
{
	clickcnt=1;
	

}
function hidecal()
{
	if(document.getElementById(divdisp))
	document.getElementById(divdisp).innerHTML="";
	divdisp=0;
	
}